/*******************************************************************************
 * CBUneItemView.m
 * ComicsBlog
 *
 * Adrien Bougouin <adrien.bougouin@gmail.com>
 */

#import "CBUneItemView.h"

@implementation CBUneItemView

@synthesize _index;

- (id) initWithFrame:(CGRect) frame
                news:(CBNews *) news
               index:(NSUInteger) index {
  self = [super initWithFrame:frame];

  if(self) {
    // add a new autorelease pool to the stack
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    UIColor *titleColor = [[UIColor alloc] initWithRed:0.0
                                                 green:0.0
                                                  blue:0.0
                                                 alpha:0.0];
    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *resources = [mainBundle bundlePath];
    CGRect imageFrame = CGRectMake(0,
                                   0,
                                   CGRectGetWidth(frame),
                                   CGRectGetHeight(frame));
    CGRect titleFrame = CGRectMake(10,
                                   CGRectGetHeight(frame) - 60,
                                   CGRectGetWidth(frame) - 20,
                                   50);
    CGRect titleBackgroundFrame = CGRectMake(0,
                                             CGRectGetHeight(frame) - 70,
                                             CGRectGetWidth(frame),
                                             70);
    NSString *backgroundUSelFilePath = [[NSString alloc] initWithFormat:@"%@/%@",
                                                                        resources,
                                                                        CBUneLabelBackgroundUSelImage];
    UIImage *backgroundUSel = [[UIImage alloc] initWithContentsOfFile:backgroundUSelFilePath];
    NSString *backgroundSelFilePath = [[NSString alloc] initWithFormat:@"%@/%@",
                                                                       resources,
                                                                       CBUneLabelBackgroundSelImage];
    UIImage *backgroundSel = [[UIImage alloc] initWithContentsOfFile:backgroundSelFilePath];
    UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:titleBackgroundFrame];
    NSString *loadingFilePath = [[NSString alloc] initWithFormat:@"%@/%@",
                                                                 resources,
                                                                 CBUneLoadingImage];
    UIImage *loadingImage = [[UIImage alloc] initWithContentsOfFile:loadingFilePath];
    UILabel *title = [[UILabel alloc] initWithFrame:titleFrame];

    _index = index;
    _imageView = [[UIImageView alloc] initWithFrame:imageFrame];
    _imageConnection = nil;
    _imageData = nil;

    // configuration
    [backgroundView setImage:backgroundUSel];
    [backgroundView setHighlightedImage:backgroundSel];
    [title setText:[news title]];
    [title setTextColor:[UIColor whiteColor]];
    [title setFont:[UIFont fontWithName:@"Francois One" size:16.5]];
    [title setBackgroundColor:titleColor];
    [title setLineBreakMode:UILineBreakModeWordWrap];
    [title setNumberOfLines:2];
    [_imageView setImage:loadingImage];

    [self addSubview:_imageView];
    [self addSubview:backgroundView];
    [self addSubview:title];
    [self loadImageFromString:[news hdImage]];

    // memory management
    [titleColor             release];
    [title                  release];
    [backgroundUSelFilePath release];
    [backgroundUSel         release];
    [backgroundSelFilePath  release];
    [backgroundSel          release];
    [backgroundView         release];
    [loadingFilePath        release];
    [loadingImage           release];

    // delete the local autorelease pool then dealloc autoreleased objects used
    // by the method
    [pool release];
  }

  return self;
}

- (void) dealloc {
  [_imageConnection cancel];

  [_imageView release];
  [_imageConnection release];
  [_imageData release];

  [super dealloc];
}

- (void) loadImageFromString:(NSString *) string {
  NSURL *imageURL = [[NSURL alloc] initWithString:string];
  NSURLRequest *imageRequest = [[NSURLRequest alloc] initWithURL:imageURL];

  _imageConnection = [[NSURLConnection alloc] initWithRequest:imageRequest
                                                     delegate:self];

  [_imageConnection start];

  // memory management
  [imageURL     release];
  [imageRequest release];
}

//==============================================================================
// NSURLConnectionDelegate
//==============================================================================

- (void) connection:(NSURLConnection *) connection
     didReceiveData:(NSData *) data {
  //update the temporary datas
  if(!_imageData) {
    _imageData = [[NSMutableData alloc] init];

    [_imageData setData:data];
  } else {
    [_imageData appendData:data];
  }
}

- (void) connection:(NSURLConnection *) connection
   didFailWithError:(NSError *) error {
  // memory management
  [_imageData release];
  _imageData = nil;
}

- (void) connectionDidFinishLoading:(NSURLConnection *) connection {
  if(_imageData) {
    UIImage *image = [[UIImage alloc] initWithData:_imageData];

    [_imageView setImage:image];

    // memory management
    [image release];
  }

  // memory management
  [_imageData release];
  _imageData = nil;
}

@end

